home *** CD-ROM | disk | FTP | other *** search
- Path: digex.net!not-for-mail
- From: jdc@access2.digex.net (John Cochran)
- Newsgroups: comp.lang.c
- Subject: Re: longjmp/setjmp madness
- Date: 24 Mar 1996 02:47:40 -0500
- Organization: Express Access Online Communications, Greenbelt, MD USA
- Message-ID: <4j2ums$1tb@access2.digex.net>
- References: <4j0llr$rvl@gap.cco.caltech.edu>
- NNTP-Posting-Host: access2.digex.net
-
- In article <4j0llr$rvl@gap.cco.caltech.edu>,
- Mika Nystroem <mika@ariadne.cs.caltech.edu> wrote:
- >
- >
- >Hi everyone,
- > I have been trying to write a nasty little program here, and had some
- >problems. I have managed to distill my problem to the following
- >unpleasantness.
- > The man pages are a little sketchy on how longjmp and setjmp are supposed
- >to work, but I don't *think* I have violated the specification. If I have,
- >please correct me on that point. (I know that what this program is doing
- >is incredibly nasty, and at least in this case, could be done much
- >nicer.. I just want to know if it is written according to specs.)
- >This program compiles and executes without problems on
- > sparc/SunOS 4.1.4
- > AXP/DEC UNIX 3.2
- >
- >And compiles and executes with a segfault upon leaving p1() on
- > i386/NetBSD 1.1A
- > i386/Linux 1.3.x
- > i386/NeXTstep
- > sparc/NetBSD 1.1B
- >
- >Ho hum.
- > Mika
- > <mika@vlsi.cs.caltech.edu>
- >
- >P.S. If you wish to respond to me, please Cc: me in email. Thanks!
- >
- >--- nasty begins here ---
- >/* ltest.c
- > mika@vlsi
- > */
- >
- >#include <setjmp.h>
- >#include <stdio.h>
- >
- >jmp_buf e1,e2,e3,e4;
- >
- >void p1()
- >{
- > if(!setjmp(e2)) longjmp(e1,1);
- > printf("p1 returning now\n"); fflush(stdout);
- > return;
- >}
- >
- >void
- >main()
- >{
- > if(!setjmp(e1)) {
- > printf("calling p1\n");
- > p1();
- > printf("returning from p1()\n");
- > exit(0);
- > }
- > printf("longjmping to p1/e2\n");
- > longjmp(e2,1);
- >}
- >
- >
-
- Sorry, but you have violated the constraints on longjmp.
-
- Quoting Section 7.6.2.1 ISO Standard C
-
- Description
- The longjmp function restores the environment saved by the most recent
- invocation of the setjmp macro in the same invocation of the program, with
- the corresponding jmp_buf argument. If there has been no such invocation, or
- if the function containing the invocation of the setjmp macro has terminated
- execution in the interim, the behaivor is undefined.
-
- ----
-
- As soon as p1() returns, jmp_buf e2 becomes invalid because function p1() has
- terminated execution. Therefore, the longjmp attempting to enter p1() will
- cause undefined behaivor.
-
- Hope this helps,
- John Cochran
-
-
-